package at.sysarch.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import at.sysarch.filter.Sink.SinkCallback;
import at.sysarch.main.IndSys;
import at.sysarch.model.Line;

/**
 * 
 * @author Andi
 *
 * Allows choosing between push / pull, starting of the pipeline and searching in the results table.
 * 
 */
public class MainFrame extends JFrame implements SinkCallback {

	private static final long serialVersionUID = 1L;
	
	private JRadioButton _pushButton;
	private JRadioButton _pullButton;
	private JButton _startButton;
	private JTable _resultTable;
	private LineTableModel _tableModel;
	private JTextField _searchField;
	
	private IndSys _indSys;
	
	public MainFrame() throws FileNotFoundException {
		
		// Initialize GUI elements
		ButtonGroup radioGroup = new ButtonGroup();
		_pullButton = new JRadioButton("Pull");
		_pushButton = new JRadioButton("Push");
		_startButton = new JButton("Start");
		JPanel topPanel = new JPanel();
		_resultTable = new JTable();
		_tableModel = new LineTableModel();		
		radioGroup = new ButtonGroup();
		_searchField = new JTextField();
		
		// Setup GUI elements
		_pushButton.setSelected(true);
		_resultTable.setModel(_tableModel);
		_startButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				_tableModel.clear();
				if (_pullButton.isSelected()) {
					
					_indSys.runSink();
				} else if (_pushButton.isSelected()) {
					
					_indSys.runSource();
				}
			}
		});
		
		_searchField.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				String query = _searchField.getText();
				
				for (int row = 0; row < _resultTable.getRowCount(); row++) {
					
					if (((String) _resultTable.getValueAt(row, 0)).toLowerCase().startsWith(query)) {
						
                        _resultTable.scrollRectToVisible(_resultTable.getCellRect(row, 0, true));
                        _resultTable.setRowSelectionInterval(row, row);
                        break;
					}
				}
			}
		});
		
		// Setup IndSys
		_indSys = new IndSys();
		_indSys.getSink().addListener(this);
		
		// Setup layout of GUI
		radioGroup.add(_pushButton);
		radioGroup.add(_pullButton);
		
		topPanel.setLayout(new GridLayout(1, 5, 10, 0));
		topPanel.add(_pushButton);
		topPanel.add(_pullButton);
		topPanel.add(_startButton);
		topPanel.add(_searchField);

		setLayout(new BorderLayout());
		add(topPanel, BorderLayout.NORTH);
		add(new JScrollPane(_resultTable), BorderLayout.CENTER);
		
		// Setup Frame
		setSize(800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void updateSinkResult(List<Line> result) {

		if (result != null) {
			_tableModel.addLines(result);
			_resultTable.revalidate();
		}
	}
}
